var immediatePaste = "";
var debugWindowStatusInterval;
var cutHTML;
var cache;

function IELoad()
{
	gameContentJS = document.getElementById(gameContentID);
	gameUtilIE.innerHTML = 'a';
		
	gameUtilIE.onmouseup = function() 
	{ 
		gameUtilIE.releaseCapture(); 
	}
	
	document.oncontextmenu = function()
	{
		if(window.event.srcElement.id == gameContentID) 
		{ 
			return false; 
		} 
		else 
		{ 
			cache = "nan"; 
		}
	}
	gameContentJS.onmousedown = IEOnRightClick;
	
	IEReturnFocusToGame();
}

function IEChangeGameSize(width, height)
{
	gameContentJS.setAttribute("width", width);
	gameContentJS.setAttribute("height", height);
	window.resizeBy(width - prevWidth, height - prevHeight );
	prevWidth = width;
	prevHeight = height;
}

function IESWFHasFocus()
{
	var result = (document.activeElement.id == 'gameContentJS');
	return result;
}

// in all of the following, if both ctrl and alt are pressed, then act as if neither were pressed
// this helps with AltGr processing: keys with AltGr come thru in IE with both ctrl and alt set. If we turn this off, then 
// they act correctly

function IEOnKeyPress()
{
	var theEvent = window.event;
	var keyCode = theEvent.keyCode;
	if (keyCode > 31 || keyCode == 13)
		gameContentJS.onKeyPressIE(keyCode, theEvent.ctrlKey && !theEvent.altKey, theEvent.altKey && !theEvent.ctrlKey, theEvent.shiftKey);
	if (gameUtilIE.innerHTML.length > 10)
		gameUtilIE.innerHTML = 'a';
}

function IEOnKeyDown()
{
	var theEvent = window.event;
	var keyCode = theEvent.keyCode;
	if (theEvent.ctrlKey || theEvent.altKey || (keyCode <= 46 && keyCode != 13) || keyCode == 113 /* F2 */ )
		gameContentJS.onKeyDownIE(keyCode, theEvent.ctrlKey && !theEvent.altKey, theEvent.altKey && !theEvent.ctrlKey, theEvent.shiftKey);
	if (IEPreventDefault(keyCode))
	{
		theEvent.keyCode = 0;
		theEvent.returnValue = false;
	}
	else if (keyCode == 8 /* backspace */ || keyCode == 46 /* del */)
		setTimeout('IEAfterKeyDown()', 0);
}
 
function IEOnRightClick()
{
	if(window.event.button > 1)
	{
		if(window.event.srcElement.id == gameContentID && cache == gameContentID) 
		{
			gameContentJS.openRightClick();
		}
		gameUtilIE.setCapture();
		if(window.event.srcElement.id)
			cache = window.event.srcElement.id;  		
	}
} 

function IEOnKeyUp()
{
	var theEvent = window.event;
	gameContentJS.onKeyUpIE(theEvent.keyCode, theEvent.ctrlKey, theEvent.altKey, theEvent.shiftKey);
}

function IEPreventDefault(keyCode)
{
	var theEvent = window.event;
	var ctrlKey = theEvent.ctrlKey && !theEvent.altKey;
	var altKey = !theEvent.ctrlKey && theEvent.altKey;
	if (!ctrlKey && keyCode == 9)	// tab
		return true;
	if (!ctrlKey && !altKey)
		return false;
	if (keyCode == 67 || keyCode == 86 || keyCode == 88)	// ctrl-c, ctrl-v, ctrl-x
		return false;
	if (keyCode == 84)										// ctrl-t
		return false;
	if (keyCode >= 65 && keyCode <= 90)
		return true;
	if (keyCode >= 37 && keyCode <= 40) // arrow keys
		return true;
	
	return false;
	
}

function IEAfterKeyDown()
{
	if (gameUtilIE.innerHTML == '')
		gameUtilIE.innerHTML = 'a';
}

function IEReturnFocusToJS()
{
	var result = true;
	try
	{
		gameUtilIE.focus();
	}
	catch (e)
	{
		if (e.name == "TypeError")
		{
			if (typeof(gameContentJS) == "object")
				gameContentJS.focus();
			result = false;
		}
	}
	return result;
}

function IEReturnFocusToGame()
{
	setTimeout('gameContentJS.focus();', 0);
}